{{- define "convert_bitmask_to_dotted_netmask" }}
  {{- $netmask := index . 1 -}}
  {{- $netmasks := dict -}}
  {{- $_ := set $netmasks "32" "255.255.255.255" -}}
  {{- $_ := set $netmasks "31" "255.255.255.254" -}}
  {{- $_ := set $netmasks "30" "255.255.255.252" -}}
  {{- $_ := set $netmasks "29" "255.255.255.248" -}}
  {{- $_ := set $netmasks "28" "255.255.255.240" -}}
  {{- $_ := set $netmasks "27" "255.255.255.224" -}}
  {{- $_ := set $netmasks "26" "255.255.255.192" -}}
  {{- $_ := set $netmasks "25" "255.255.255.128" -}}
  {{- $_ := set $netmasks "24" "255.255.255.0" -}}
  {{- $_ := set $netmasks "23" "255.255.254.0" -}}
  {{- $_ := set $netmasks "22" "255.255.252.0" -}}
  {{- $_ := set $netmasks "21" "255.255.248.0" -}}
  {{- $_ := set $netmasks "20" "255.255.240.0" -}}
  {{- $_ := set $netmasks "19" "255.255.224.0" -}}
  {{- $_ := set $netmasks "18" "255.255.192.0" -}}
  {{- $_ := set $netmasks "17" "255.255.128.0" -}}
  {{- $_ := set $netmasks "16" "255.255.0.0" -}}
  {{- $_ := set $netmasks "15" "255.254.0.0" -}}
  {{- $_ := set $netmasks "14" "255.252.0.0" -}}
  {{- $_ := set $netmasks "13" "255.248.0.0" -}}
  {{- $_ := set $netmasks "12" "255.240.0.0" -}}
  {{- $_ := set $netmasks "11" "255.224.0.0" -}}
  {{- $_ := set $netmasks "10" "255.192.0.0" -}}
  {{- $_ := set $netmasks "9" "255.128.0.0" -}}
  {{- $_ := set $netmasks "8" "255.0.0.0" -}}
  {{- $_ := set $netmasks "7" "254.0.0.0" -}}
  {{- $_ := set $netmasks "6" "252.0.0.0" -}}
  {{- $_ := set $netmasks "5" "248.0.0.0" -}}
  {{- $_ := set $netmasks "4" "240.0.0.0" -}}
  {{- $_ := set $netmasks "3" "224.0.0.0" -}}
  {{- $_ := set $netmasks "2" "192.0.0.0" -}}
  {{- $_ := set $netmasks "1" "128.0.0.0" -}}
  {{- $_ := set $netmasks "0" "0.0.0.0" -}}

  {{- if lt (len $netmask) 3 -}}
    {{- get $netmasks $netmask -}}
  {{- else -}}
    {{- $netmask -}}
  {{- end -}}

{{- end -}}


{{- define "convert_dotted_netmask_to_bitmask" }}
  {{- $netmask := index . 1 -}}
  {{- $netmasks := dict -}}
  {{- $_ := set $netmasks "255.255.255.255" "32" -}}
  {{- $_ := set $netmasks "255.255.255.254" "31" -}}
  {{- $_ := set $netmasks "255.255.255.252" "30" -}}
  {{- $_ := set $netmasks "255.255.255.248" "29" -}}
  {{- $_ := set $netmasks "255.255.255.240" "28" -}}
  {{- $_ := set $netmasks "255.255.255.224" "27" -}}
  {{- $_ := set $netmasks "255.255.255.192" "26" -}}
  {{- $_ := set $netmasks "255.255.255.128" "25" -}}
  {{- $_ := set $netmasks "255.255.255.0" "24" -}}
  {{- $_ := set $netmasks "255.255.254.0" "23" -}}
  {{- $_ := set $netmasks "255.255.252.0" "22" -}}
  {{- $_ := set $netmasks "255.255.248.0" "21" -}}
  {{- $_ := set $netmasks "255.255.240.0" "20" -}}
  {{- $_ := set $netmasks "255.255.224.0" "19" -}}
  {{- $_ := set $netmasks "255.255.192.0" "18" -}}
  {{- $_ := set $netmasks "255.255.128.0" "17" -}}
  {{- $_ := set $netmasks "255.255.0.0" "16" -}}
  {{- $_ := set $netmasks "255.254.0.0" "15" -}}
  {{- $_ := set $netmasks "255.252.0.0" "14" -}}
  {{- $_ := set $netmasks "255.248.0.0" "13" -}}
  {{- $_ := set $netmasks "255.240.0.0" "12" -}}
  {{- $_ := set $netmasks "255.224.0.0" "11" -}}
  {{- $_ := set $netmasks "255.192.0.0" "10" -}}
  {{- $_ := set $netmasks "255.128.0.0" "9" -}}
  {{- $_ := set $netmasks "255.0.0.0" "8" -}}
  {{- $_ := set $netmasks "254.0.0.0" "7" -}}
  {{- $_ := set $netmasks "252.0.0.0" "6" -}}
  {{- $_ := set $netmasks "248.0.0.0" "5" -}}
  {{- $_ := set $netmasks "240.0.0.0" "4" -}}
  {{- $_ := set $netmasks "224.0.0.0" "3" -}}
  {{- $_ := set $netmasks "192.0.0.0" "2" -}}
  {{- $_ := set $netmasks "128.0.0.0" "1" -}}
  {{- $_ := set $netmasks "0.0.0.0" "0" -}}

  {{- if gt (len $netmask) 3 -}}
    {{- get $netmasks $netmask -}}
  {{- else -}}
    {{- $netmask -}}
  {{- end -}}

{{- end -}}


{{- define "get_network_address" }}
  {{- $network := index . 1 -}}
  {{- index ( splitList "/" ($network) ) 0 -}}
{{- end -}}


{{- define "get_network_mask" }}
  {{- $network := index . 1 -}}
  {{- index ( splitList "/" ($network) ) 1 -}}
{{- end -}}


{{- define "get_dotted_network_mask" }}
  {{- $network := index . 1 -}}
  {{- $netmask := index ( splitList "/" ($network) ) 1 -}}
  {{- include "convert_bitmask_to_dotted_netmask" (list . $netmask) -}}
{{- end -}}


{{- define "get_network_with_bitmask" }}
  {{- $network := index . 1 -}}
  {{- $netmask := index ( splitList "/" ($network) ) 1 -}}
  {{- $address := include "get_network_address" (list . $network) }}
  {{- $mask := include "convert_dotted_netmask_to_bitmask" (list . $netmask) -}}
  {{- $address }}/{{ $mask }}
{{- end -}}


{{- define "push_route_format" }}
  {{- $route := index . 1 -}}
  {{- $address := index ( splitList "/" ($route) ) 0 -}}
  {{- $mask := index ( splitList "/" ($route) ) 1 -}}
  {{- $address }} {{ include "convert_bitmask_to_dotted_netmask" (list . $mask) -}}
{{- end -}}
